/*
 * #{copyright}#
 */

package io.choerodon.hap.job.examples;

import java.util.Date;

import io.choerodon.hap.job.AbstractJob;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobKey;
import org.quartz.PersistJobDataAfterExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * 无状态的JOB举例.
 * <p>
 * A job dumb job that will throw a job execution exception.
 * </p>
 * 
 * @author shiliyan
 *
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class BadJob2 extends AbstractJob {

    // Logging
    private static Logger log = LoggerFactory.getLogger(BadJob2.class);
    private int calculation;

    /**
     * 
     * Empty public constructor for job initialization.
     */
    public BadJob2() {
    }

    @Override
    public void safeExecute(JobExecutionContext context) throws Exception {

        JobKey jobKey = context.getJobDetail().getKey();
        log.info("---" + jobKey + " executing at " + new Date());

        // a contrived example of an exception that
        // will be generated by this job due to a
        // divide by zero error
        try {
            int zero = 0;
            calculation = 4815 / zero;
        } catch (Exception e) {
            log.info("--- Error in job!");

            throw e;
        }

        log.info("---" + jobKey + " completed at " + new Date());

    }

    @Override
    public boolean isRefireImmediatelyWhenException() {
        return false;
    }

}
